package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.*;
import com.luo.service.ClassService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/1/17 17:40
 */
@WebServlet("/queryAllOneTwoClass")
public class ListClassOneTwoAll extends HttpServlet {

    ClassService classService = new ClassService();

    @Override
    protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        String oneClassId = req.getParameter("oneClassId");
        ResultObj result = new ResultObj();
        result.setCode("1");
        result.setMsg("查询成功");
        if (oneClassId == null || oneClassId == ""){
            //查询一级分类
            List<OneClass> oneClasses = classService.queryAllOneClass();
            result.setResult(oneClasses);
        }else{
            //查询二级分类
            OneClass oneClass = new OneClass();
            oneClass.setId(Integer.parseInt(oneClassId));
            List<OneTwoClass> oneTwoClasses = classService.queryOneTwoClassByOneId(oneClass);
            result.setResult(oneTwoClasses);
        }
        String jsonString = JSON.toJSONString(result);
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }
}
